package com.kr.aia.powerrp.common;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.kr.aia.powerrp.R;

import android.content.Context;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Toast;

public class VideoPlayController extends SurfaceView implements SurfaceHolder.Callback {
	private MediaPlayer mp = null;
	private SurfaceHolder surfaceHolder;
	private String fileName;

	public VideoPlayController(Context context, String fileName) {
		super(context);
		this.fileName = fileName;
		
		//this.getHolder().setFixedSize(480, 360);
		
		/*
		android.view.ViewGroup.LayoutParams lp = this.getLayoutParams();
		lp.width = 480;
	    lp.height = 360;

	    this.setLayoutParams(lp);
	    */
		
		mp = new MediaPlayer();
		surfaceHolder = getHolder();
		surfaceHolder.addCallback(this);
		surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		
		mp.setOnCompletionListener(fnCompletionListener);
	}
	
	
    //===================
    // click listener
    //===================
	private onTestCompletionListener _testCompletionListener;
	
    public interface onTestCompletionListener
    {
        public void onCompletion(MediaPlayer arg0);
    }
    
    private OnCompletionListener fnCompletionListener = new OnCompletionListener()
    {
    	@Override
		public void onCompletion(MediaPlayer arg0) {
			// TODO Auto-generated method stub
    		_testCompletionListener.onCompletion(arg0);
		}
    };
    
    public void setOnCompletionListener(onTestCompletionListener listener)
    {
    	_testCompletionListener = listener;
    }
    
    
    
    
	
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
		//startCamera(holder, width, height);
	}
	
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		try{
			Log.e("PRP", fileName + " ---- VideoController!!!!!!!!!!!!!!!!!!!!!!!");
		    mp.setDataSource(fileName);
		    mp.setDisplay(surfaceHolder);
		    
		    mp.prepare();
		    mp.start();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		mp.release();
		//mCamera.stopPreview();
        //mCamera.release();
	}
}
